<template>
	<div class="footer-bar">
		<!--回到首页-->
		<div class="back-home" @click="$router.push('/index')">
			<svg-icon iconName="shouye"></svg-icon>
			<p>首页</p>
		</div>

		<!--按钮组-->
		<div class="btn-group">
			<div class="button share" @click="toShare">
				<h3>分享好友</h3>
				<p>赚¥{{ goodsInfo.shareMoney | Fixed(2) }}</p>
			</div>
			<div class="button coupon" @click="getPrivilege">
				<h3>领券购买</h3>
				<p>省¥{{ (Number(goodsInfo.allowanceMoney) + goodsInfo.couponMoney) | Fixed(2) }}</p>
			</div>
		</div>

		<authorize-dig ref="authorizeDig"></authorize-dig>

		<!--复制成功-->
		<van-popup class="copy-popup" get-container="body" v-model="visible" closeable>
			<div class="main">
				<svg-icon class="icon" iconName="copy-ok"></svg-icon>
				<p class="title">
					淘口令复制成功<br />
					打开【淘宝】购买吧
				</p>
			</div>
		</van-popup>
	</div>
</template>

<script>
import { Popup, Toast } from 'vant';
import { AuthorizeDig } from '@/components';
import { mapGetters } from 'vuex';
export default {
	name: 'FooterBar',
	components: {
		AuthorizeDig,
		'van-popup': Popup
	},
	props: {
		goodsInfo: {
			type: Object
		}
	},
	data() {
		return {
			visible: false,
			tkl: null
		};
	},
	computed: {
		...mapGetters(['userInfo'])
	},
	methods: {
		toShare() {
			if (this.userInfo.tbStatus === 1) {
				if (Number(this.$route.query.superGoods) === 1) {
					this.$router.push({
						path: '/goods-share',
						query: {
							superGoods: 1
						}
					});
				} else {
					this.$router.push({
						path: '/goods-share',
						query: {
							gid: this.goodsInfo.gid
						}
					});
				}
			} else {
				if (this.userInfo.tbStatus === 2) {
					this.$toast('亲亲，您的淘宝账号需补充支付宝实名认证信息，请先去认证哦！');
				}
				if (this.userInfo.tbStatus === 3) {
					this.$toast('授权过期，请重新授权');
				}
				this.$refs.authorizeDig.open('/index');
			}
		},
		// 获取淘口令
		getPrivilege() {
			if (this.userInfo.tbStatus === 1) {
				Toast.loading({
					duration: 0, // 持续展示 toast
					forbidClick: true,
					message: '正在生成淘口令...'
				});
				let params = {
					title: this.goodsInfo.title,
					itemId: this.goodsInfo.gid
				};
				this.$service.shopping.taobao.privilege(params).then(res => {
					if (res.tkl) {
						this.tkl = res.tkl;
					} else {
						this.tkl = res.coupon_click_url;
					}

					// 复制淘口令
					// let input = document.createElement("input");
					// input.value = this.tkl;
					// document.body.appendChild(input);
					// input.select();
					// document.execCommand("Copy");
					// document.body.removeChild(input);

					window.BestpayHtml5.System.setClipboardContent(
						{
							data: this.tkl
						},
						() => {
							Toast.clear();
							this.visible = true;
							setTimeout(() => {
								this.visible = false;
							}, 5000);
						}
					);
				});
			} else {
				if (this.userInfo.tbStatus === 2) {
					this.$toast('亲亲，您的淘宝账号需补充支付宝实名认证信息，请先去认证哦！');
				}
				if (this.userInfo.tbStatus === 3) {
					this.$toast('授权过期，请重新授权');
				}
				this.$refs.authorizeDig.open('/index');
			}
		}
	},
	destroyed() {
		Toast.clear();
	}
};
</script>

<style lang="scss" scoped>
.footer-bar {
	width: 100%;
	height: 128px;
	padding: 24px 10px;
	background: $color-white;
	position: fixed;
	bottom: 0;
	box-shadow: 0 0 12px #ccc;
	@include flexBox(space-between);
	.back-home {
		width: 100px;
		@include flexBox(null, center, column);
		svg {
			width: 50px;
			height: 48px;
		}
		p {
			font-size: 20px;
			color: #242348;
		}
	}
	.btn-group {
		border-radius: 50px;
		overflow: hidden;
		display: flex;
		.button {
			width: 288px;
			height: 80px;
			color: $color-white;
			@include flexBox(center, center, column);
			h3 {
				font-size: 28px;
				font-weight: 400;
			}
			p {
				font-size: 22px;
			}
			&.share {
				background: #29231e;
			}
			&.coupon {
				background: #29231e;
				background: linear-gradient(225deg, #f77301, $main-color);
			}
		}
	}
}
.copy-popup {
	width: 452px;
	height: 336px;
	border-radius: 16px;
	.main {
		width: 100%;
		height: 100%;
		@include flexBox(center, center, column);
		svg {
			width: 110px;
			height: 110px;
		}
		.title {
			font-size: 28px;
			color: #2e2d2d;
			text-align: center;
			margin: 30px 0;
		}
	}
}
// 判断iphoneX 将 footerBox 的 padding-bottom 填充到最底部
@supports (bottom: env(safe-area-inset-bottom)) {
	.footer-bar {
		padding-bottom: env(safe-area-inset-bottom);
	}
}
</style>
